package com.example.cr.insect.service;

import cn.hutool.core.bean.BeanUtil;
import com.example.cr.common.response.PageResponse;
import com.example.cr.insect.entity.PreventionLog;
import com.example.cr.insect.entity.PreventionLogExample;
import com.example.cr.insect.mapper.PreventionLogMapper;
import com.example.cr.insect.request.PreventionLogListRequest;
import com.example.cr.insect.response.PreventionLogResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
    import cn.hutool.core.date.DateTime;
    import cn.hutool.core.util.ObjectUtil;
    import com.example.cr.common.util.SnowflakeUtil;
    import com.example.cr.insect.request.PreventionLogRequest;

@Service
public class PreventionLogService {
private static final Logger log = LoggerFactory.getLogger(PreventionLogService.class);
@Autowired
PreventionLogMapper preventionLogMapper;

public PageResponse<PreventionLogResponse> list(PreventionLogListRequest request) {
    PreventionLogExample preventionLogExample = new PreventionLogExample();
    preventionLogExample.setOrderByClause("id desc");

    // 处理搜索条件
    PreventionLogExample.Criteria criteria = preventionLogExample.createCriteria();
    if (request.getKeyword() != null && !request.getKeyword().isEmpty()) {
    String keyword = "%" + request.getKeyword() + "%";
    }

    PageHelper.startPage(request.getPage(), request.getSize());
    log.info("当前页码 current_page = {}", request.getPage());
    log.info("每页记录数 per_page = {}", request.getSize());

    List<PreventionLog> preventionLogs = preventionLogMapper.selectByExample(preventionLogExample);

    PageInfo<PreventionLog> pageInfo = new PageInfo<>(preventionLogs);
    log.info("总记录数 total = {}", pageInfo.getTotal());
    log.info("总页数 pages = {}", pageInfo.getPages());

    List<PreventionLogResponse> list = BeanUtil.copyToList(preventionLogs, PreventionLogResponse.class);

        PageResponse<PreventionLogResponse> pageResponse = new PageResponse<>();
            pageResponse.setTotal(pageInfo.getTotal());
            pageResponse.setList(list);

            return pageResponse;
            }
            public void save(PreventionLogRequest request) {
            PreventionLog preventionLog = BeanUtil.copyProperties(request, PreventionLog.class);
            DateTime now = DateTime.now();
            if (ObjectUtil.isNull(preventionLog.getId()) || preventionLog.getId() == 0L) {
            preventionLog.setId(SnowflakeUtil.getId());
            preventionLog.setCreatedAt(now);
            preventionLog.setUpdatedAt(now);
            preventionLogMapper.insert(preventionLog);
            } else {
            preventionLog.setUpdatedAt(now);
            preventionLogMapper.updateByPrimaryKeySelective(preventionLog);
            }
            }

            public int deleteBatch(List<Long> ids) {
                PreventionLogExample preventionLogExample = new PreventionLogExample();
                PreventionLogExample.Criteria criteria = preventionLogExample.createCriteria();
                criteria.andIdIn(ids);
                return preventionLogMapper.deleteByExample(preventionLogExample);
                }
                }
